package com.mindskip.xzs.repository;

import com.mindskip.xzs.domain.other.KeyValue;
import com.mindskip.xzs.domain.Question;
import com.mindskip.xzs.repository.base.MyMapper;
import com.mindskip.xzs.viewmodel.admin.question.QuestionPageRequestVM;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

@Mapper
@Repository
public interface QuestionMapper extends MyMapper<Question> {
//    int deleteByPrimaryKey(Integer id);
//
//    int insert(Question record);
//
//    int insertSelective(Question record);
//
//    Question selectByPrimaryKey(Integer id);
//
//    int updateByPrimaryKeySelective(Question record);
//
//    int updateByPrimaryKey(Question record);

    String queryParams = " id, question_type questionType, subject_id subjectId, score, grade_level gradeLevel, difficult, correct, " +
            "info_text_content_id infoTextContentId, create_user createUser, status, create_time createTime, deleted ";

    @Select("<script>" +
            "select " + queryParams +
            "FROM t_question " +
            "where deleted=0 " +
            "<if test=\"id != null \">\n" +
            "and id= #{id}\n" +
            "</if>\n" +
            "<if test=\"level != null \">\n" +
            "and grade_level= #{level}\n" +
            "</if>\n" +
            "<if test=\"subjectId != null \">\n" +
            "and subject_id= #{subjectId}\n" +
            "</if>\n" +
            "<if test=\"questionType != null \">\n" +
            "and question_type= #{questionType}\n" +
            "</if>" +
            "</script>")
    List<Question> page(QuestionPageRequestVM requestVM);

    @Select("<script>" +
            "select " + queryParams+
            " FROM t_question where id in " +
            "<foreach item=\"id\" collection=\"ids\" open=\"(\" separator=\",\" close=\")\">\n" +
            "#{id}\n" +
            "</foreach>" +
            "</script>")
    List<Question> selectByIds(@Param("ids") List<Integer> ids);

    @Select("SELECT count(*) from t_question where  deleted=0")
    Integer selectAllCount();

    @Select("SELECT create_time as name,COUNT(create_time) as value from\n" +
            "(\n" +
            "SELECT DATE_FORMAT(create_time,'%Y-%m-%d') as create_time from t_question\n" +
            "WHERE  deleted=0  and  create_time  between  #{startTime}  and  #{endTime}\n" +
            ") a\n" +
            "GROUP BY create_time")
    List<KeyValue> selectCountByDate(@Param("startTime") Date startTime, @Param("endTime") Date endTime);
}
